﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Tetris_Online___Client.FormElements
{
    class TextBox
    {
        public Vector2 Position;
        public String Text;

        public Color BackgroundColor;
        public Color FontColor;

        private float length;
        private bool showCursor = true;
        private double time = 0;

        public TextBox(Vector2 Position, int Length, SpriteFont font)
        {
            this.Position = Position;
            BackgroundColor = Color.White;
            FontColor = Color.Black;
            Text = "";
            string sample = "";
            for (int i = 0; i < Length; i++)
                sample += " ";
            length = font.MeasureString(sample).X;
        }

        public void Update(KeyboardState keyState, KeyboardState oldKeyState)
        {
            bool shift = keyState.IsKeyDown(Keys.LeftShift) || keyState.IsKeyDown(Keys.RightShift);
            foreach (Keys key in keyState.GetPressedKeys())
            {
                if (oldKeyState.IsKeyUp(key))
                {
                    if (key == Keys.Back && Text.Length > 0)
                    {
                        Text = Text.Remove(Text.Length - 1, 1);
                    }
                    else if (key == Keys.Enter)
                    {
                        Basic.currentScreen = new SinglePlayerScreen();
                    }
                    else
                    {
                        Text += keyToString(key, shift);
                    }
                }
            }

            time += Basic.gameTime.ElapsedGameTime.TotalMilliseconds;
            if (time >= 1000)
            {
                time = 0;
            }
            if (time >= 500)
            {
                showCursor = true;
            }
            else
                showCursor = false;
        }

        public void Render2D(SpriteBatch spriteBatch, SpriteFont font)
        {
            //Textbox
            Basic.spriteBatch.Draw(Texture.fill, new Rectangle((int)Position.X, (int)Position.Y - 3, (int)length, 35), BackgroundColor);
            Basic.spriteBatch.DrawString(font, Text, new Vector2((int)Position.X, (int)Position.Y), FontColor);
            //|
            if (showCursor)
                Basic.spriteBatch.DrawString(font, "|", new Vector2((int)(font.MeasureString(Text).X + Position.X), (int) Position.Y), FontColor);
        }

        private string keyToString(Keys key, bool shifted)
        {
            string keyString = "";
            switch (key)
            {
                case Keys.A: keyString = "a"; break;
                case Keys.B: keyString = "b"; break;
                case Keys.C: keyString = "c"; break;
                case Keys.D: keyString = "d"; break;
                case Keys.E: keyString = "e"; break;
                case Keys.F: keyString = "f"; break;
                case Keys.G: keyString = "g"; break;
                case Keys.H: keyString = "h"; break;
                case Keys.I: keyString = "i"; break;
                case Keys.J: keyString = "j"; break;
                case Keys.K: keyString = "k"; break;
                case Keys.L: keyString = "l"; break;
                case Keys.M: keyString = "m"; break;
                case Keys.N: keyString = "n"; break;
                case Keys.O: keyString = "o"; break;
                case Keys.P: keyString = "p"; break;
                case Keys.Q: keyString = "q"; break;
                case Keys.R: keyString = "r"; break;
                case Keys.S: keyString = "s"; break;
                case Keys.T: keyString = "t"; break;
                case Keys.U: keyString = "u"; break;
                case Keys.V: keyString = "v"; break;
                case Keys.W: keyString = "w"; break;
                case Keys.X: keyString = "x"; break;
                case Keys.Y: keyString = "y"; break;
                case Keys.Z: keyString = "z"; break;
                case Keys.D0: keyString = "0"; break;
                case Keys.D1: keyString = "1"; break;
                case Keys.D2: keyString = "2"; break;
                case Keys.D3: keyString = "3"; break;
                case Keys.D4: keyString = "4"; break;
                case Keys.D5: keyString = "5"; break;
                case Keys.D6: keyString = "6"; break;
                case Keys.D7: keyString = "7"; break;
                case Keys.D8: keyString = "8"; break;
                case Keys.D9: keyString = "9"; break;
                case Keys.NumPad0: keyString = "0"; break;
                case Keys.NumPad1: keyString = "1"; break;
                case Keys.NumPad2: keyString = "2"; break;
                case Keys.NumPad3: keyString = "3"; break;
                case Keys.NumPad4: keyString = "4"; break;
                case Keys.NumPad5: keyString = "5"; break;
                case Keys.NumPad6: keyString = "6"; break;
                case Keys.NumPad7: keyString = "7"; break;
                case Keys.NumPad8: keyString = "8"; break;
                case Keys.NumPad9: keyString = "9"; break;
                case Keys.OemPeriod: keyString = "."; break;
                case Keys.Subtract: keyString = "-"; break;
                case Keys.OemMinus: keyString = "-"; break;
                case Keys.Add: keyString = "+"; break;
                case Keys.Space: keyString = " "; break;
            }

            if (shifted)
            {
                keyString = keyString.ToUpper();
                if (keyString == ".")
                    keyString = ":";
            }
            return keyString;
        }
    }
}
